/*
 *    Copyright (c) 2018-2025, lengleng All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: lengleng (wangiegie@gmail.com)
 */

package com.pig4cloud.pigx.user.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pig4cloud.pigx.common.core.util.R;
import com.pig4cloud.pigx.common.customize.enums.AbnormalEnum;
import com.pig4cloud.pigx.common.customize.exception.DuckweedException;
import com.pig4cloud.pigx.common.log.annotation.SysLog;
import com.pig4cloud.pigx.user.dto.FuelAccountPageDTO;
import com.pig4cloud.pigx.user.entity.FuelAccount;
import com.pig4cloud.pigx.user.service.FuelAccountService;
import com.pig4cloud.pigx.user.vo.LoginUserFuelVO;
import org.springframework.security.access.prepost.PreAuthorize;
import com.pig4cloud.pigx.common.excel.annotation.ResponseExcel;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.math.RoundingMode;
import java.util.List;

/**
 * 燃料账户
 *
 * @author pigx code generator
 * @date 2022-11-23 14:43:55
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/fuel_account" )
@Api(value = "fuelaccount", tags = "燃料账户管理")
public class FuelAccountController {

    private final  FuelAccountService fuelAccountService;

    /**
     * 分页查询
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @PostMapping("/page" )
    public R<Page<FuelAccount>> getFuelAccountPage(@RequestBody FuelAccountPageDTO dto) {
		try {
			Page<FuelAccount> page = fuelAccountService.page(new Page<FuelAccount>() {{
				setCurrent(dto.getCurrent());
				setSize(dto.getSize());
			}}, new QueryWrapper<FuelAccount>() {{
				if (dto.getUserId() != null) {
					like("user_id", dto.getUserId());
				}
				orderByDesc("create_time");
			}});
			for (FuelAccount record : page.getRecords()) {
				record.setBalance(record.getBalance().setScale(6, RoundingMode.DOWN).stripTrailingZeros());
			}
			return R.ok(page);
		} catch (Exception e) {
			throw new DuckweedException(AbnormalEnum.MSG.DISCONNECTED);
		}
    }

	/**
	 * 获取当前登陆人燃料余额
	 * @return r
	 */
	@GetMapping("/app")
	public R<LoginUserFuelVO> getUserFuel() {
		return fuelAccountService.getUserFuel();
	}


    /**
     * 通过id查询燃料账户
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('user_fuelaccount_view')" )
    public R getById(@PathVariable("id" ) Long id) {
        return R.ok(fuelAccountService.getById(id));
    }

    /**
     * 新增燃料账户
     * @param fuelAccount 燃料账户
     * @return R
     */
    @ApiOperation(value = "新增燃料账户", notes = "新增燃料账户")
    @SysLog("新增燃料账户" )
    @PostMapping
    @PreAuthorize("@pms.hasPermission('user_fuelaccount_add')" )
    public R save(@RequestBody FuelAccount fuelAccount) {
        return R.ok(fuelAccountService.save(fuelAccount));
    }

    /**
     * 修改燃料账户
     * @param fuelAccount 燃料账户
     * @return R
     */
    @ApiOperation(value = "修改燃料账户", notes = "修改燃料账户")
    @SysLog("修改燃料账户" )
    @PutMapping
    @PreAuthorize("@pms.hasPermission('user_fuelaccount_edit')" )
    public R updateById(@RequestBody FuelAccount fuelAccount) {
        return R.ok(fuelAccountService.updateById(fuelAccount));
    }

    /**
     * 通过id删除燃料账户
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id删除燃料账户", notes = "通过id删除燃料账户")
    @SysLog("通过id删除燃料账户" )
    @DeleteMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('user_fuelaccount_del')" )
    public R removeById(@PathVariable Long id) {
        return R.ok(fuelAccountService.removeById(id));
    }


    /**
     * 导出excel 表格
     * @param fuelAccount 查询条件
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('user_fuelaccount_export')" )
    public List<FuelAccount> export(FuelAccount fuelAccount) {
        return fuelAccountService.list(Wrappers.query(fuelAccount));
    }

}
